﻿package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import server.ComActionInterface;
import server.PersonInterface;

import client.Configuration;

import java.awt.Dialog.ModalityType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JInternalFrame;
import java.awt.Canvas;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;


public class ComHouseGUI extends JDialog {

	private JPanel contentPane;
	Person p = new Person();
	int pid=p.getPID();
	

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ComHouseGUI frame = new ComHouseGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public ComHouseGUI() {
		setTitle("\u5BB6\u91CC");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 632, 490);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("\u6D17\u6F31");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		btnNewButton.setForeground(Color.WHITE);
	    btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Configuration config = new Configuration();
				String rmiAddress=config.getRmiAddress();
					
					try {
						
						ComActionInterface comAction = (ComActionInterface) Naming.lookup(rmiAddress+"ComActionList");
						int h = comAction.getHP("wash");
						System.out.print(h);
						int ha = comAction.getHap("wash");
						
						PersonInterface person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
						boolean z = person.hpChanges(pid, h, '-');
						System.out.println(z);
						boolean y = person.hapChanges(pid, ha, '+');
						if(z&&y)
						   JOptionPane.showMessageDialog(null, "您减少了"+h+"点HP，增加了"+ha+"点Hap");
						else
					       JOptionPane.showMessageDialog(null, "您的HP不足");
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
					
				
				
			}
		});
	    btnNewButton.setContentAreaFilled(false);
	    btnNewButton.setBorder(null);
		btnNewButton.setBounds(35, 309, 122, 36);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("\u5403\u996D");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		button.setForeground(Color.WHITE);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ComEatGUI ceg = new ComEatGUI();
				ceg.setLocationRelativeTo(null);
				ceg.setVisible(true);
				
			}
		});
		
		button.setContentAreaFilled(false);
		button.setBorder(null);
		button.setBounds(255, 376, 122, 36);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u5A31\u4E50");
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		button_1.setForeground(Color.WHITE);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Configuration config = new Configuration();
				String rmiAddress=config.getRmiAddress();
				ComActionInterface comAction;
					
					try {
						comAction = (ComActionInterface) Naming.lookup(rmiAddress+"ComActionList");
						int h = comAction.getHP("play");
						int ha = comAction.getHap("play");
						System.out.print(h);
						PersonInterface person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
						boolean z = person.hpChanges(pid, h, '-');
						boolean y = person.hapChanges(pid, ha, '+');
						if(z&&y)
						JOptionPane.showMessageDialog(null, "您减少了"+h+"点HP，增加了"+ha+"点Hap");
						else
							JOptionPane.showMessageDialog(null,  "您的HP不足");
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
					
			}
		});
		button_1.setContentAreaFilled(false);
		button_1.setBorder(null);
		button_1.setBounds(255, 309, 122, 36);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("\u4F11\u606F");
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		button_2.setForeground(Color.WHITE);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Configuration config = new Configuration();
				String rmiAddress=config.getRmiAddress();
				ComActionInterface comAction;
					
					try {
						comAction = (ComActionInterface) Naming.lookup(rmiAddress+"ComActionList");
						int h = comAction.getHP("rest");
						int ha = comAction.getHap("rest");
						System.out.print(h);
						PersonInterface person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
						boolean z = person.hpChanges(pid, h, '-');
						boolean y = person.hapChanges(pid, ha, '+');
						JOptionPane.showMessageDialog(null, "您增加了"+h+"点HP，增加了"+ha+"点Hap");
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
					
			}
		});
		button_2.setContentAreaFilled(false);
		button_2.setBorder(null);
		button_2.setBounds(35, 376, 122, 36);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("\u4E32\u95E8");
		button_3.setFont(new Font("微软雅黑", Font.PLAIN, 25));
		button_3.setForeground(Color.WHITE);
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Configuration config = new Configuration();
				String rmiAddress=config.getRmiAddress();
				ComActionInterface comAction;
					
					try {
						comAction = (ComActionInterface) Naming.lookup(rmiAddress+"ComActionList");
						int h = comAction.getHP("visit");
						int ha = comAction.getHap("visit");
						System.out.print(h);
						PersonInterface person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
						boolean z = person.hpChanges(pid, h, '-');
						boolean y = person.hapChanges(pid, ha, '+');
						if(z)
						JOptionPane.showMessageDialog(null, "您减少了"+h+"点HP，增加了"+ha+"点Hap");
						else
							JOptionPane.showMessageDialog(null,  "您的HP不足");
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
					
			}
		});
		button_3.setContentAreaFilled(false);
		button_3.setBorder(null);
		button_3.setBounds(471, 309, 122, 36);
		contentPane.add(button_3);
		
		JButton btnNewButton_1 = new JButton("\u51FA\u95E8");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ComComeinGUI ccg = new ComComeinGUI();
				ccg.setLocationRelativeTo(null);
				ccg.setVisible(true);
				
			}
		});
		btnNewButton_1.setBounds(513, 419, 93, 23);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel = new JLabel(new ImageIcon("src/client/images/h.jpg"));
		lblNewLabel.setBounds(0, -21, 616, 489);
		contentPane.add(lblNewLabel);
	}
}
